
import csv


def transform():
    """
        csv转换坐标系
    """
    # 指定要打开的CSV文件的路径
    file_path = 'aks.csv'

    file = open('aks_new.csv',
                'a+', encoding='utf-8')

    file.write('名称,大类,中类,经度,纬度,省份,城市,区域\n')

    # 以'utf-8'编码打开CSV文件
    with open(file_path, mode='r', encoding='utf-8') as csv_file:
        # 创建CSV阅读器对象
        csv_reader = csv.reader(csv_file)
        # 遍历CSV文件中的每一行
        for row in csv_reader:
            # 在这里处理每一行的数据。例：将行数据打印出来
            # lon = row[3]
            # lat = row[4]
            # glon, glat = gcj02towgs84(float(lon), float(lat))
            # row[3] = str(glon)
            # row[4] = str(glat)
            # 写入test.csv文件
            file.write(",".join(row)+'\n')

    # 关闭文件
    file.close()


def multCSV(folder_path):
    """
        批量合并csv
        folder_path: 指定CSV文件所在的文件夹路径
    """
    # 创建一个空列表来存储所有CSV文件的数据行
    all_data = []

    # 遍历文件夹中的每个CSV文件并合并它们
    for filename in os.listdir(folder_path):
        if filename.endswith('.csv'):
            file_path = os.path.join(folder_path, filename)
            with open(file_path, 'r', newline='', encoding='utf-8') as csv_file:
                csv_reader = csv.reader(csv_file)
                # 跳过每个CSV文件的标题行，只在第一个文件中添加标题
                if not all_data:
                    all_data.extend([row for row in csv_reader])
                else:
                    next(csv_reader)  # 跳过标题行
                    all_data.extend([row for row in csv_reader])

    # 将合并后的数据保存为一个新的CSV文件
    output_file_path = 'merged_data.csv'
    with open(output_file_path, 'w', newline='', encoding='utf-8') as output_file:
        csv_writer = csv.writer(output_file)
        csv_writer.writerows(all_data)
